package org.soderiakelvy.model.dao.imp;

import java.io.Serializable;
import java.util.List;


import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.soderiakelvy.model.dao.FrioCalorDao;
import org.soderiakelvy.model.domain.FrioCalorVO;
import org.soderiakelvy.model.util.HibernateUtil;

public class FrioCalorDaoImp implements FrioCalorDao, Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void insertFrioCalorVO(FrioCalorVO frioCalor){
		try {
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			session.save(frioCalor);
			session.getTransaction().commit();
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void updateFrioCalorVO(FrioCalorVO frioCalor) throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.update(frioCalor);
		session.getTransaction().commit();
		session.close();
		
	}

	@Override
	public void deleteFrioCalorVO(FrioCalorVO frioCalor) throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.delete(frioCalor);
		session.getTransaction().commit();
		session.close();
		
	}

	@Override
	public List<FrioCalorVO> getAllFrioCalorVO() throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(FrioCalorVO.class);
		List<FrioCalorVO> listado = criteria.list();
		session.close();
		return listado;
	}

	@Override
	public FrioCalorVO getFrioCalorByNumSerie(String numeroDeSerie) throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(FrioCalorVO.class);
		criteria.add(Restrictions.eq("numeroDeSerie", numeroDeSerie));
		FrioCalorVO fcVO =(FrioCalorVO) criteria.uniqueResult();		
		session.close();
		return fcVO;
	}

	@Override
	public List<FrioCalorVO> getFrioCalorByTipoAndEstado(String tipo, String estado) throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(FrioCalorVO.class);
		criteria.add(Restrictions.eq("aguaDeRed", tipo));
		criteria.add(Restrictions.eq("estado", estado));
		List<FrioCalorVO> equiposFC = criteria.list();
		session.close();
		return equiposFC;
	}

	@Override
	public List<FrioCalorVO> searchFrioCalor(FrioCalorVO frioCalor) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session  = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(FrioCalorVO.class);
    	Disjunction busquedaOr = Restrictions.disjunction();
    	busquedaOr.add(Restrictions.like("numeroDeSerie", frioCalor.getNumeroDeSerie()));
    	busquedaOr.add(Restrictions.eq("marca", frioCalor.getMarca()));
    	busquedaOr.add(Restrictions.eq("modelo", frioCalor.getModelo()));
    	criteria.add(busquedaOr);
    	List<FrioCalorVO> result = criteria.list();
		session.close();
		return result;
	}

	@Override
	public List<FrioCalorVO> getFrioCalorAguaDeRed() {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(FrioCalorVO.class);
		criteria.add(Restrictions.eq("aguaDeRed", "SI"));
		List<FrioCalorVO> result = criteria.list();
		session.close();
		return result;
	}

	@Override
	public List<FrioCalorVO> getEquiposFrioCalorByFrioCalor(
			FrioCalorVO frioCalor, String tipo, String estado) throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session  = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(FrioCalorVO.class);
		
		Disjunction busquedaOr = Restrictions.disjunction();
		if(frioCalor.getNumeroDeSerie() != null && !frioCalor.getNumeroDeSerie().isEmpty()){
			busquedaOr.add(Restrictions.like("numeroDeSerie", frioCalor.getNumeroDeSerie(),MatchMode.START));
		}
		if(frioCalor.getMarca() != null && !frioCalor.getMarca().isEmpty()){
			busquedaOr.add(Restrictions.like("marca", frioCalor.getMarca(),MatchMode.START));
		}
		if(frioCalor.getModelo() != null && !frioCalor.getModelo().isEmpty()){
			busquedaOr.add(Restrictions.like("modelo", frioCalor.getModelo(),MatchMode.START));
		}
		criteria.add(Restrictions.eq("aguaDeRed", tipo));
    	criteria.add(Restrictions.eq("estado", estado));
    	criteria.add(busquedaOr);
    	
    	List<FrioCalorVO> result = criteria.list();
		session.close();
		
		return result;
	}

}
